package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import negocio.Disciplina;
import negocio.Professor;
import controle.Fachada;
import excecoes.DisciplinaComMesmoNomeException;

/**
 * Servlet implementation class Proc_cadastro_disciplina
 */
public class Proc_cadastro_disciplina extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Proc_cadastro_disciplina() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String matProfessor = request.getParameter("matProfessor");
		String nome = request.getParameter("nome");
		String cargaHoraria = request.getParameter("cargaHoraria");
		Professor p = new Professor();
		p.setMatricula(Integer.parseInt(matProfessor));
		Disciplina d = new Disciplina();
		d.setNomeDisciplina(nome);
		d.setProfessor(p);
		d.setCargaHoraria(Integer.parseInt(cargaHoraria));

		Fachada fac = Fachada.obterInstancia();
		
		//insere no banco...
		try {
			fac.inserirDisciplina(d);
			request.setAttribute("script1", "<script type='text/javascript'>alert('Cadastro de Disciplina realizado com sucesso!'); location='../cadDisciplina.jsp'; </script>");
			getServletContext().getRequestDispatcher("/cadDisciplina.jsp").forward(request, response);
		} catch (DisciplinaComMesmoNomeException e) {
			request.setAttribute("script1", "<script type='text/javascript'>alert('"+e.getMessage()+"'); location='../cadDisciplina.jsp'; </script>");
			getServletContext().getRequestDispatcher("/cadDisciplina.jsp").forward(request, response);
		}
		
	}

}
